<?php
/**
 * Note: 渠道CPA关联信息
 * Date: 2021/12/29
 */
namespace App\Models\Game\Center;

use App\Traits\ModelTrait;

class AppSourceCpa extends Base
{
    use ModelTrait;

    public const CHANNEL_KUAISHOU = 'kuaishou';
    public const CHANNEL_DOUYIN = 'douyin';
    public const CHANNEL_BAIDU = 'baidu';
    public const CHANNEL_TENCENT = 'tencent';
    public const CHANNEL_UC = 'uc';
    public const CHANNEL_CUSTOM = 'custom';

    protected $table = 'app_source_cpa';

    public $timestamps = false;

    public $fillable = [
        'source',
        'channel',
        'advertiser_id',
        'advertiser_name',
        // 'campaign_id',
        // 'campaign_name',
        'rebate',
        'formula',
    ];

    // 优先匹配长单词, 防止前缀替换
    protected $formulaFields = [
        '$payChannelFee' => '支付通道费',
        '$pay' => '充值',
        '$withdraw' => '权益',
        '$registerUser' => '注册',
        '$thirdExchange' => '积分消耗',
    ];

    protected $appends = [
        'formula_text',
    ];

    /**
     * 中文转英文
     * @param string $formulaText
     * @return string
     */
    public static function convertFormula(string $formulaText): string
    {
        $obj = new self();
        return str_replace(array_values($obj->formulaFields), array_keys($obj->formulaFields), $formulaText);
    }

    /**
     * 英文转中文
     * @param $value
     * @return array|mixed|string|string[]
     */
    public function getFormulaTextAttribute($value)
    {
        return str_replace(array_keys($this->formulaFields), array_values($this->formulaFields), $this->formula);
    }

    /**
     * 中文转英文
     * @param $value
     * @return void
     */
    public function setFormulaAttribute($value)
    {
        $this->attributes['formula'] = self::convertFormula($value);
    }
}
